var ImageFileUpload = function (src, inputId, imageId, hiddenId) {
    var self = this;
    if (!src) {
        $("#" + imageId).attr('src', contextRoot + 'resources/uploader/AjaxFileUpload/upload_img.png');
    }
    else {
        $("#" + imageId).attr('src', contextRoot + "file/downloadImage?filename=" + src);
    }
    self.upload_cover = function () {
        self.ajax_upload(inputId, function (data) {
            if (data.success) {
                $('#' + imageId).attr('src', contextRoot + "file/downloadImage?filename=" + data.data);
                $("#" + hiddenId).val(data.data);
            }
        });
    };
    self.ajax_upload = function (feid, callback) { //具体的上传图片方法
        // if (self.image_check(feid)) { //自己添加的文件后缀名的验证
            $.ajaxFileUpload({
                fileElementId: feid,
                url: contextRoot + "file/uploadFile", //后台方法的路径
                type: 'post',
                dataType: 'json',
                secureuri: false,
                async: true,
                success: function (data) {
                    self.bind_change();
                    if (callback) callback.call(this, data);
                },
                error: function (data, status, e) {
                    self.bind_change();
                }
            });
        // }
    };
    self.image_check = function (feid) {
        var img = document.getElementById(feid);
        return /.(jpg|png|gif|bmp)$/.test(img.value) ? true : (function () {
            alert('图片格式仅支持jpg、png、gif、bmp格式，且区分大小写。');
            return false;
        })();
    };
    self.bind_change = function () {
        $("#" + inputId).bind("change", function () {
            self.upload_cover($("#" + inputId));
        });
    }

    self.bind_change();
}

var ImageFileUpload_head = function (src, inputId, imageId, hiddenId,schoolId,studentId) {
    var self = this;
    if (!src) {
        $("#" + imageId).attr('src', contextRoot + 'resources/uploader/AjaxFileUpload/upload_img.png');
    }
    else {
        $("#" + imageId).attr('src', contextRoot + "file/downloadImage?filename=" + src);
    }
    self.upload_cover = function () {
        self.ajax_upload(inputId, function (data) {
            if (data.success) {
                $('#' + imageId).attr('src', contextRoot + "file/downloadImage?filename=" + data.data);
                $("#" + hiddenId).val(data.data);
            }
        });
    };
    self.ajax_upload = function (feid, callback) { //具体的上传图片方法
        // if (self.image_check(feid)) { //自己添加的文件后缀名的验证
        $.ajaxFileUpload({
            fileElementId: feid,
            url: contextRoot + "file/uploadFile_head?schoolId="+schoolId+"&studentId="+studentId, //后台方法的路径
            type: 'post',
            dataType: 'json',
            secureuri: false,
            async: true,
            success: function (data) {
                self.bind_change();
                if (callback) callback.call(this, data);
                $("#" + imageId).attr('src', contextRoot + data.data);
                $("#" + hiddenId).val(data.data);

            },
            error: function (data, status, e) {
                self.bind_change();
            }
        });
        // }
    };
    self.image_check = function (feid) {
        var img = document.getElementById(feid);
        return /.(jpg|png|gif|bmp)$/.test(img.value) ? true : (function () {
            alert('图片格式仅支持jpg、png、gif、bmp格式，且区分大小写。');
            return false;
        })();
    };
    self.bind_change = function () {
        $("#" + inputId).bind("change", function () {
            self.upload_cover($("#" + inputId));
        });
    }
    self.bind_change();
}

var ImageFileUpload2 = function (src, inputId, imageId, hiddenId) {
    var self = this;
    if (src) {
        $("#" + imageId).attr('src', contextRoot + "file/downloadImage?filename=" + src);
    }
}

var FileUpload = function (src, inputId,  hiddenId) {
    var self = this;
    if (!src) {
    }
    else {
    }
    self.upload_cover = function () {
        self.ajax_upload(inputId, function (data) {
            if (data.success) {
                // $('#' + imageId).attr('src', contextRoot + "file/downloadImage?filename=" + data.data);
                $("#" + hiddenId).val(data.data);
            }
        });
    };
    self.ajax_upload = function (feid, callback) { //具体的上传图片方法
        // if (self.image_check(feid)) { //自己添加的文件后缀名的验证
        $.ajaxFileUpload({
            fileElementId: feid,
            url: contextRoot + "file/uploadFile", //后台方法的路径
            type: 'post',
            dataType: 'json',
            secureuri: false,
            async: true,
            success: function (data) {
                console.log(data);
                self.bind_change();
                if (callback) callback.call(this, data);
            },
            error: function (data, status, e) {
                self.bind_change();
            }
        });
        // }
    };
    self.image_check = function (feid) {
        var img = document.getElementById(feid);
        return /.(jpg|png|gif|bmp)$/.test(img.value) ? true : (function () {
            alert('图片格式仅支持jpg、png、gif、bmp格式，且区分大小写。');
            return false;
        })();
    };
    self.bind_change = function () {
        $("#" + inputId).bind("change", function () {
            self.upload_cover($("#" + inputId));
        });
    }

    self.bind_change();
}